<!--
 * @Author: hucc
 * @Date: 2021-10-13 16:24:38
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-13 18:54:47
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <script src="../js/vue-router.js"></script>
    <style>
        header {
            height: 50px;
            background-color: aqua;
        }
        
        main {
            display: flex;
            height: 700px;
        }
        
        .left {
            width: 150px;
            border: 3px solid orange;
            margin: 10px 10px 0 0;
        }
        
        .right {
            /* flex: 1; */
            width: calc(100% - 150px);
            border: 3px solid blue;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <header>我是网站头部</header>
        <main>
            <div class="left">
                <!-- 4.路由的使用 -->
                <router-link to="/user">用户管理</router-link>
                <br>
                <router-link to="/student">学生管理</router-link>
                <br>
                <router-link to="/class">班级管理</router-link>
            </div>
            <div class="right">
                <!-- 5.为组件加载提供容器窗口 -->
                <!-- router-view 实现了组件之间的动态加载 -->
                <router-view></router-view>
            </div>
        </main>
    </div>
    <script>
        //1.注册组件
        let com1 = {
            data() {
                return {

                }
            },
            template: `
              <div>
               用户管理
                </div>
            `
        }
        let com2 = {
            data() {
                return {

                }
            },
            methods: {
                clickHandler(e) {
                    if (e.target.innerText == '成绩管理') {
                        if (this.$route.fullPath != '/student/grade') {
                            // this.$router.push({
                            //     name: 'grade',
                            // })
                            this.$router.push({
                                    name: 'grade',
                                    params: {
                                        id: 1001,
                                        name: 'zs'

                                    }
                                })
                                //this.$router.replace('/student/grade');
                                //this.$router.go(1);
                        }
                    } else {
                        if (this.$route.fullPath != '/student/register') {
                            // this.$router.push({
                            //     name: 'register',
                            // })
                            this.$router.push({
                                path: '/student/register',
                                query: {
                                    id: 1002,
                                    name: 'lisi',

                                }
                            })
                        }

                    }




                }
            },
            template: `
              <div>
                学生管理
                
                <div>
                <button @click='clickHandler'>成绩管理</button>
                <button @click='clickHandler'>学籍管理</button>
                <button @click='$router.go(-1)'>返回</button>
                </div>
                <router-view></router-view>
                </div>
                
            `
        }
        let com3 = {
            data() {
                return {

                }
            },
            template: `
              <div>
                班级管理
                </div>
            `
        }
        let comGrade = {
            data() {
                return {

                }
            },
            template: `
              <div>
                成绩管理
                </div>
            `,
            created() {
                console.log('params', this.$route.params);
            }
        }
        let comRegister = {
                data() {
                    return {

                    }
                },
                template: `
              <div>
                学籍管理
                </div>
            `,
                created() {
                    console.log('query', this.$route.query);
                }
            }
            //2.定义路由
        let router = new VueRouter({
            //修改路由模式 默认模式hash
            mode: 'history', //静态服务器打开
            routes: [{
                path: '/user',
                component: com1
            }, {
                path: '/student',
                component: com2,
                children: [{
                    path: 'grade',
                    component: comGrade,
                    name: 'grade'
                }, {
                    path: 'register',
                    component: comRegister,
                    name: 'register'
                }, ]
            }, {
                path: '/class',
                component: com3
            }, ]
        })
        new Vue({
            el: '#app',
            data: {},
            methods: {},
            //3.路由注册
            router: router
        })
    </script>
</body>

</html>